package com.sixteen.theGreat.workbench.web.controller;

import com.sixteen.theGreat.commons.constants.Constants;
import com.sixteen.theGreat.commons.utils.DateUtil;
import com.sixteen.theGreat.commons.utils.UUIDUtil;
import com.sixteen.theGreat.settings.entity.User;
import com.sixteen.theGreat.workbench.entity.Talk;
import com.sixteen.theGreat.workbench.entity.TalkRemark;
import com.sixteen.theGreat.workbench.service.TalkRemarkService;
import com.sixteen.theGreat.workbench.service.TalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ActivityController {
    @Autowired
    private TalkService talkService;
    @Autowired
    private TalkRemarkService talkRemarkService;

    /**
     * 新建说说
     * @param talkContent 说说内容
     * @param request
     * @return 返回结果封装到的实体类
     */
    @PostMapping("/workbench/main/addTalk.do")
    @ResponseBody
    public Object addTalk(String talkContent, HttpServletRequest request){
        User user= (User) request.getSession().getAttribute(Constants.SESSION_USER);
        Talk talk = Talk.builder().talkId(UUIDUtil.getUUID())
                .talkName(user.getUserName() + "的说说")
                .timeCreate(DateUtil.getCurrentDateTime())
                .userId(user.getUserId())
                .talkContent(talkContent).build();
        return talkService.addTalk(talk);
    }

    /**
     * 查询我的说说
     * @param request
     * @return
     */
    @GetMapping("workbench/main/queryMyTalk.do")
    @ResponseBody
    public List<Talk> queryMyTalk(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(Constants.SESSION_USER);
        return talkService.queryTalkByUserId(user.getUserId());
    }

    /**
     * 删除我的说说
     * @return
     */
    @PostMapping("/workbench/main/deleteTalk.do")
    @ResponseBody
    public Object deleteTalk(String[] talkId){
        return talkService.removeTalk(talkId);
    }

    /**
     * 查询好友说说
     * @param request
     * @return
     */
    @GetMapping("/workbench/main/queryFriendTalkByHostId.do")
    @ResponseBody
    public List<Talk> queryFriendTalkByHostId(HttpServletRequest request){
        User sessionUser= (User) request.getSession().getAttribute(Constants.SESSION_USER);
        return talkService.queryFriendTalkByHostId(sessionUser.getUserId());
    }

    /**
     * 查询我的说说以及 我的好友的说说
     * @param request
     * @return
     */
    @GetMapping("/workbench/main/queryAllTalkByUserId.do")
    @ResponseBody
    public List<Talk> queryAllTalkByUserId(HttpServletRequest request){
        User sessionUser= (User) request.getSession().getAttribute(Constants.SESSION_USER);
        return talkService.queryAllTalkByUserId(sessionUser.getUserId());
    }

    /**
     * 添加说说的评论
     * @param request
     * @param talkId
     * @param remarkContent
     * @return
     */
    @PostMapping("/workbench/main/addTalkRemark.do")
    @ResponseBody
    public Object addTalkRemark(HttpServletRequest request,String talkId,String remarkContent){
        User sessionUser= (User) request.getSession().getAttribute(Constants.SESSION_USER);
        TalkRemark talkRemark = TalkRemark.builder().remarkId(UUIDUtil.getUUID())
                .remarkContent(remarkContent)
                .timeRemark(DateUtil.getCurrentDateTime())
                .talkId(talkId)
                .userId(sessionUser.getUserId()).build();
        return talkRemarkService.addTalkRemark(talkRemark);
    }
}
